package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class GetSessionUtil {
    private static final String USER_SESSION_NAME =  "USER_IN_SESSION";
    private static final String PERMISSION_SESSION_NAME =  "PERMISSION_IN_SESSION";


    //获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    //获取user对象
    public static Employee getCurrentUser(){
        return (Employee)getSession().getAttribute(GetSessionUtil.USER_SESSION_NAME);
    }
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(GetSessionUtil.USER_SESSION_NAME,employee);
    }

    //获取设置permission对象
    public static List<Permission> getCurrentPermission(){
        return (List<Permission>)getSession().getAttribute(GetSessionUtil.PERMISSION_SESSION_NAME);
    }
    public static void setCurrentPermission(List<Permission> permissions){
        getSession().setAttribute(GetSessionUtil.PERMISSION_SESSION_NAME,permissions);
    }
}
